<?php
namespace Teleplay\Controller;
use Common\Controller\TeleplayController;
class IndexController extends TeleplayController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }



    /**
     * 获取题目
     */
    public function getquestions(){

        $member_id = I('post.member_id','','int');
        $state = I('post.state','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }
        $info =  M('teleplay_member')->field('money,rankname')->where(array('teleplay_member_id'=>$member_id))->find();
        //一次取20题
        $nextcustoms = 50;
        $count = M('teleplay_questions')->count();
        $data['info'] = array(
            'money'=>$info['money'],
            'count'=>$count,
            'rankname'=>$info['rankname']
        );
        if ($state=='2'){
            $sort = I('post.customs','','int');
            if (!$sort){
                $this->returnData('','参数错误',401);
            }

            $question = M('teleplay_questions')->field('app,third_id,tips',true)->limit($sort,$nextcustoms)->order('customs asc')->select();

            foreach ($question as $key=>$value){

                $question[$key]['question'] = $this->url.$question[$key]['question'];
                $question[$key]['video_url'] = $this->url.$question[$key]['video_url'];
            }
            $data['question'] = $question;

            $this->returnData($data);

        }

        $customs = M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('customs');



        $question = M('teleplay_questions')->field('app,third_id,tips',true)->limit($customs,$nextcustoms)->order('customs asc')->select();

        foreach ($question as $key=>$value){

            $question[$key]['question'] = $this->url.$question[$key]['question'];
            $question[$key]['video_url'] = $this->url.$question[$key]['video_url'];
        }


        $data['question'] = $question;

        $this->returnData($data);
    }

    /**
     * 答对一题
     */
    public function inanwser(){

        $member_id = I('post.member_id','','int');

        $state = I('post.state','','int');

        $customs = I('post.customs','','int');

        if (!$member_id||!$state||!$customs){
            $this->returnData('','参数错误',401);
        }

        if ($customs>=0){
            $rankname = '路人甲';
        }

        if ($customs>=45){
            $rankname = '跑龙套';
        }

        if ($customs>=137){
            $rankname = '小配角';
        }

        if ($customs>=260){
            $rankname = '演艺新秀';
        }

        if ($customs>=300){
            $rankname = '节目主持人';
        }

        if ($customs>=367){
            $rankname = '二线明星';
        }

        if ($customs>=450){
            $rankname = '担当主角';
        }

        if ($customs>=520){
            $rankname = '重头主角';
        }

        if ($customs>=600){
            $rankname = '大牌明星';
        }

        if ($customs>=680){
            $rankname = '超级明星';
        }

        $nowweek = (int)date('W');
        $year = date('Y');

        $where = array(
            'year'=>$year,
            'week'=>$nowweek,
            'teleplay_member_id'=>$member_id,
        );

        //判断当周是否有挑战记录
        $ischange = M('teleplay_week')->field('teleplay_member_id')->where($where)->find();


        if ($ischange){
            M('teleplay_week')->where($where)->setInc('customs');//当周分数加一

            M('teleplay_week')->where($where)->save(array('exittime'=>date('Y-m-d H:i:s')));
        }else{
            $where['createtime'] = date('Y-m-d H:i:s');
            $where['exittime'] = date('Y-m-d H:i:s');
            $where['customs'] = 1;
          M('teleplay_week')->data($where)->add();
        }
        $money =  M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('money');
        $money = $money+10;
        $update = array(
            'customs'=>$customs,
            'rankname'=>$rankname,
            'money'=>$money,
        );
        M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->save($update);

        $this->returnData($update,'成功',200);


    }

    /**
     * 提示
     */
    public function reminder(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }

        $money = M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('money');

        if ($money<30){
            $this->returnData(array('enough'=>'no'),'金币不足',100);
        }
        M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->setDec('money',30);

        $data = array(
            'money'=>$money-30,
            'enough'=>'yes',
        );

        $this->returnData($data);

    }

    /**
     * 签到
     */
    public function  signin(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','非法操作',401);
        }

        $today  = date('Y-m-d');

        $where['signin_time'] = $today;
        $where['teleplay_member_id'] = $member_id;

        $signin_num = M('teleplay_signin')->where($where)->getField('signin_num');

        if (!empty($signin_num)){
            M('teleplay_signin')->where($where)->setInc('signin_num');
            $this->returnData(array('signin'=>'yes'),'已签到',200);
        }

        $where['signin_num'] = 1;

        M('teleplay_signin')->data($where)->add();

        M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->setInc('money',20);

        $money =  M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('money');

        $res = array(
            'money'=>$money,
            'count'=>20,
        );

        $this->returnData($res,'签到成功增加20金币',200);
    }


    /**
     * 排行
     */
    public function rank(){

        $member_id = I('post.member_id','','int');
        $state = I('post.state','1','int');


        //世界排行
        if ($state=='2'){
            $rank = $this->getcacherank();

            $this->returnData($rank);
        }

        if (!$member_id||!$state){
            $this->returnData('','非法操作',401);
        }

        //好友排行
        if ($state=='1'){

            $where['my_member_id'] = $member_id;


            $coupling_uids = M('teleplay_relationship')->where($where)->getField('coupling_member_id',true);
            if (empty($coupling_uids)){
                $coupling_uids = array();
            }
            $coupling_uids[] = $member_id;


            $uids = M('teleplay_relationship')->where(array('coupling_member_id'=>$member_id))->getField('my_member_id',true);
            if (empty($uids)){
                $uids = array();
            }

            $ids = array_unique(array_merge($coupling_uids,$uids));//合并去重

            $where1['teleplay_member_id'] = array('in',$ids);
            $rank = "";
            if ($ids){
                $rank = M('teleplay_member')->field('nickname,avatarurl,customs,rankname')->where($where1)->limit(30)->order('customs desc')->select();

            }

            $this->returnData($rank,'成功','200');

        }

    }

    /**
     * 广告
     */
    public function advertising(){

        $appid = I('post.appid','','htmlspecialchars');

        $data = M('teleplay_appshow')->field('content,title',true)->where(array('isshow'=>'1'))->select();

        foreach ($data as $key=>$val){
            $data[$key]['logo'] = $this->url.$data[$key]['logo'];
            $data[$key]['index'] = $key;
        }

        $this->returnData($data);
    }

    /**
     * 更多好玩
     */
    public function moreplay(){

        $data = array(
            'appid'=>'wx6b1c7907c5733fc9',
            'url'=>'/pages/index/index',
        );

        $this->returnData($data);

    }

    /**
     * 微信分享记录
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');

        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }


        $setinfo['sharegroup'] = 30;

        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }


        $info = M('teleplay_member')->field('appid,session_key')->where(array('teleplay_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];
        //解析openGId
        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'){
            $this->returnData('','分享失败',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['teleplay_member_id'] = $member_id;
        $where['opengid'] = $openGId;

        $record = M('teleplay_wechat_group')->where($where)->getField('sharenum'); //判断是否已分享

            if ($record>=3){
            M('teleplay_wechat_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'out'),'今天该群分享次数过多',201);
        }

        if ($record){
            M('teleplay_wechat_group')->where($where)->setInc('sharenum');
            M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->setInc('money',$setinfo['sharegroup']);

            $mymoney =  M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('money');

            $data['money'] = $mymoney;
            $data['repetition'] = 'no';
            $this->returnData($data,'分享成功加30金币',200);
        }

        if (!$record||empty($record)){
            //如果没分享过则插入，用户增加金币
            $add = array(
                'opengid'=>$openGId,
                'sharetime'=>$time,
                'sharenum'=>1,
                'teleplay_member_id'=>$member_id,
            );

            M('teleplay_wechat_group')->data($add)->add();
            M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->setInc('money',$setinfo['sharegroup']);

            $mymoney =  M('teleplay_member')->where(array('teleplay_member_id'=>$member_id))->getField('money');

            $data['money'] = $mymoney;
            $data['repetition'] = 'no';
            $this->returnData($data,'分享成功加30金币',200);
        }
    }



    /**
     * 好友关联
     */
    public function friendsassociated(){

        $uid = I('post.my_member_id','','int');
        $coupling_uid = I('post.coupling_member_id','','int');
        $appid = I('post.appid','','htmlspecialchars');

        if ($uid==""||$coupling_uid==""){
            $this->returnData('','参数不能为空',401);
        }

        if ($uid==$coupling_uid){
            $this->returnData('','自己不用关联自己',100);
        }


        $where['my_member_id']=$uid;
        $where['coupling_member_id']=$coupling_uid;
        $where['appid'] = $appid;

        $where1['my_member_id']=$coupling_uid;
        $where1['coupling_member_id']=$uid;
        $where1['appid'] = $appid;

        $relevancy = M('teleplay_relationship')->field('teleplay_relationship_id')->where($where)->find();

        $relevancy1 = M('teleplay_relationship')->field('teleplay_relationship_id')->where($where1)->find();
        //判断是否已关联(有待优化)
        if ($relevancy||$relevancy1){
            $this->returnData('','已关联',200);
        }

        $data = array(
            'my_member_id'=>$uid,
            'coupling_member_id' =>$coupling_uid,
            'appid'=>$appid,
            'createtime' =>time(),
        );

        $insert = M('teleplay_relationship')->data($data)->add();

        if ($insert){
            $this->returnData('','关联成功',200);
        }else{
            $this->returnData('','关联失败',100);
        }

    }


    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');
        $member_id = I('post.member_id','','int');
        $template_id = I('post.template_id','1','htmlspecialchars');

        if ($touser==""||$form_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
            'teleplay_member_id'=>$member_id,
        );
        $where['touser'] = $touser;
        $where['createtime'] = $date;
        $where['status'] = '-1';

        $ishave = M('teleplay_template_send')->field('wxopen_template_send_id')->where($where)->find();

        if ($ishave){
            M('teleplay_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }else{
            M('teleplay_template_send')->data($insert)->add();
        }

        $this->returnData('成功','200');

    }


}